package com.hzz.service.report;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzz.api.report.IReportBimonthlyService;
import com.hzz.dao.report.IReportBimonthlyDao;
import com.hzz.dto.report.ReportBimonthlyDto;
import com.hzz.entity.report.ReportBimonthly;
import com.hzz.framework.Response;
import com.hzz.framework.SysErrorCode;
import com.hzz.framework.annotation.FeignService;
import com.hzz.framework.exception.ServiceException;
import com.hzz.framework.service.BaseService;
import com.hzz.framework.util.CommonConstant;
import com.hzz.framework.util.IdUtil;
import com.hzz.framework.util.StrUtil;
import com.hzz.framework.util.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@FeignService
@Slf4j
public class ReportBimonthlyService extends BaseService implements IReportBimonthlyService {
    @Autowired
    IReportBimonthlyDao reportBimonthlyDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public Response saveOrUpdateData(ReportBimonthlyDto dto) throws Exception {
        Response result = new Response(0, "success");
        try {
            ReportBimonthly entity = copyTo(dto, ReportBimonthly.class);
//            UserDto userDto = JwtUtil.getSubject(UserDto.class);
//            entity.setReportingRiveruserName(userDto.getName());
//            entity.setReportingOrgId(userDto.getOrgId());
//            entity.setReportingOrgName(userDto.getOrgName());
            //判断数据是否存在
            if (reportBimonthlyDao.isDataExist(entity) != 0) {
                //数据存在
                reportBimonthlyDao.update(entity);
            } else {
//                entity.setReportingRiveruserId(userDto.getId());
//                entity.setReportingDate(new Date());
                //新增
                if (!ValidatorUtil.notEmpty(entity.getId())) {
                    entity.setId(StrUtil.asString(IdUtil.nextId()));
                }
                reportBimonthlyDao.insert(entity);
                result.data = entity.getId();
            }
        } catch (Exception e) {
            log.error("信息保存异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public String deleteData(ReportBimonthlyDto dto) throws Exception {
        String result = "success";
        try {
            ReportBimonthly entity = copyTo(dto, ReportBimonthly.class);
            if (reportBimonthlyDao.deleteByPrimaryKey(entity) == 0) {
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("物理删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = CommonConstant.DB_DEFAULT_TIMEOUT, rollbackFor = {Exception.class, RuntimeException.class})
    public String deleteDataById(ReportBimonthlyDto dto) throws Exception {
        String result = "success";
        try {
            ReportBimonthly entity = copyTo(dto, ReportBimonthly.class);
            if (reportBimonthlyDao.deleteById(entity) == 0) {
                throw new RuntimeException("数据不存在!");
            }
        } catch (Exception e) {
            log.error("逻辑删除异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }

    @Override
    public PageInfo findDataIsPage(ReportBimonthlyDto dto) throws Exception {
        PageInfo pageInfo = null;
        try {
            ReportBimonthly entity = copyTo(dto, ReportBimonthly.class);
            PageHelper.startPage(PN(dto.getPageNum()), PS(dto.getPageSize()));
            List list = reportBimonthlyDao.findDataIsPage(entity);
            pageInfo = new PageInfo(list);
            pageInfo.setList(copyTo(pageInfo.getList(), ReportBimonthlyDto.class));
        } catch (Exception e) {
            log.error("信息[分页]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return pageInfo;
    }

    @Override
    public List<ReportBimonthlyDto> findDataIsList(ReportBimonthlyDto dto) throws Exception {
        List<ReportBimonthlyDto> results = null;
        try {
            ReportBimonthly entity = copyTo(dto, ReportBimonthly.class);
            results = copyTo(reportBimonthlyDao.findDataIsList(entity), ReportBimonthlyDto.class);
        } catch (Exception e) {
            log.error("信息[列表]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return results;
    }

    @Override
    public ReportBimonthlyDto findDataById(ReportBimonthlyDto dto) throws Exception {
        ReportBimonthlyDto result = null;
        try {
            ReportBimonthly entity = copyTo(dto, ReportBimonthly.class);
            result = copyTo(reportBimonthlyDao.selectByPrimaryKey(entity), ReportBimonthlyDto.class);
        } catch (Exception e) {
            log.error("信息[详情]查询异常!", e);
            throw new ServiceException(SysErrorCode.defaultError, e.getMessage());
        }
        return result;
    }


}
